<?php

namespace Admin\Controller;

use Think\Controller;

class AdminController extends CommonController
{

    public function ajaxLst()//lst ,用户管理
    {
        $model_admin = D('admin');
        /*
         * l : 单页条数
         * p : 页数
         */
        $l = I('get.limit');
        $l = !$l ? 6 : $l;

        /*
         * 对每页显示的条数和页数进行处理
         */
        $p = I('get.page');
        $p = $p <= 0 ? 1 : $p;
        $p = ($p - 1) * $l;//(页码 - 1 ) * 单页条数

        $res = $model_admin->query("SELECT * FROM admin limit $p,$l");// 从第$p条开始 取$n条
        $count = $model_admin->query("SELECT count(1)  AS num FROM admin ");

        $count[0]['num'];
        $code = !$res ? self::RET_FAILED : self::RET_SUCCESS;
        $msg = !$res ? '暂无信息' : '提示信息';
        $this->ajaxRet($code, $msg, $res, $count[0]['num']);

        // $this->ajaxReturn(array('data' => $res, 'count' => $count[0]['num'], 'code' => empty($res) ? 1 : 0, 'msg' => empty($res) ? '暂无信息' : '提示信息'));

    }

    public function file()//上传头像
    {
        if ($_FILES['file']['tmp_name'] == '') {
            $this->ajaxRet(self::RET_FAILED, '上传失败');
        }
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'image', 'JPEG');// 设置附件上传类型
        $upload->rootPath = $_SERVER['DOCUMENT_ROOT']; // 设置附件上传根目录
        $upload->savePath = '/Public/Portarait/'; // 设置附件上传（子）目录
        // 上传单个文件
        $info = $upload->uploadOne($_FILES['file']);
        if (!$info) {
            $this->ajaxRet(self::RET_FAILED, $upload->getError());
        }
        $fileName = $info['savepath'] . $info['savename'];// 上传成功 获取上传文件信息

        $this->ajaxRet(self::RET_SUCCESS, "上传成功", ["fileName" => $fileName]);
    }

    public function add()//添加用户信息
    {

        $model_admin = D('admin');
        if (IS_POST) {
            $data['username'] = I('post.username');
            $data['password'] = I('post.password');
            $data['pic'] = I('post.pic');
            $data['phone'] = I('post.phone');
            $data['email'] = I('post.email');
            if ($model_admin->create($data)) {
                if ($model_admin->add()) {
                    $this->success('添加管理员成功！', U('Admin/lst'));
                } else {
                    $this->arror('添加管理员失败！');
                }
            } else {
                $this->error($model_admin->getError());
            }

            return;

        }

        $this->display();
    }

    public function del()//删除用户
    {
        $post = $_POST;
        $model_del = D('admin');
        $model_del->where("id=$post[id]")->delete();
        $count = $model_del->query('SELECT count(1) AS num FROM admin');
        $this->ajaxRet(self::RET_SUCCESS, '删除成功', $model_del, $count[0]['num']);
    }

    public function up()//修改用户信息
    {
        $post = $_POST;
        $model_up = D('admin');
        $model_up->where("id=$post[id]")->save($post);
    }

    public function checkPwd()// 安全设置 验证密码
    {
        $data['password'] = $_POST['password'];
        $data['id'] = session('user')[0]['id'];
        $rule = where_model()->parseWhere($data);

        $model_checkpwd = D('admin');
        $res = $model_checkpwd->query("SELECT * FROM admin where $rule ");

        $code = !$res ? self::RET_FAILED : self::RET_SUCCESS;
        $msg = !$res ? '密码错误' : '密码正确';
        $this->ajaxRet($code, $msg);
    }

    public function secure()//安全设置 添加
    {
        $model_secure = D('encrypted');
        if (IS_POST) {
            $data['quiz'] = $_POST['quiz'];
            $data['quiz_answer'] = $_POST['quiz_answer'];
            $data['admin_id'] = $_POST['admin_id'];
            if ($model_secure->add($data)) {
                $this->ajaxRet(self::RET_FAILED, '设置成功');
            } else {
                $this->ajaxRet(self::RET_SUCCESS, '设置失败');
            }
        }
        $this->display();
    }

    public function checkEncrypted()//安全设置 验证用户是否已经设置密保
    {
        $admin_id = $_POST['admin_id'];
        $model_check_admin = D('encrypted');
        $sql = sprintf("SELECT * FROM encrypted WHERE admin_id = '%s' ", $admin_id);
        $res = $model_check_admin->query($sql);

//        $res = $model_check_admin->query("SELECT * FROM encrypted WHERE admin_id = '$admin_id'");

        $code = !$res ? self::RET_FAILED : self::RET_SUCCESS;
        $msg = !$res ? '继续完成设置' : '已经设置过了';
        $this->ajaxRet($code, $msg);
    }

    public function checkUsername()// 添加管理员 验证用户名是否存在
    {
        $name = $_POST['username'];
        $model_check_user = D('admin');
        $sql = sprintf("SELECT * FROM admin WHERE username = '%s' ", $name);
        $user = $model_check_user->query($sql);

        $code = !$user ? self::RET_FAILED : self::RET_SUCCESS;
        $msg = !$user ? '用户名可以使用' : '该用户名已经存在';
        $this->ajaxRet($code, $msg);

    }

    public function checkPhone()// 添加管理员 验证手机号是否存在
    {
        $phone = $_POST['phone'];
        $model_check_phone = D('admin');
        $sql = sprintf("SELECT * FROM admin WHERE phone = '%s' ", $phone);
        $tal = $model_check_phone->query($sql);
        $code = !$tal ? self::RET_FAILED : self::RET_SUCCESS;
        $msg = !$tal ? '号码可以使用' : '该号码已经存在';
        $this->ajaxRet($code, $msg);
    }

    public function checkEmail()// 添加管理员 验证邮箱是否存在
    {
        $email = $_POST['email'];
        $model_check_e = D('admin');
        $sql = sprintf("SELECT * FROM admin WHERE email = '%s' ", $email);
        $em = $model_check_e->query($sql);
        $code = !$em ? self::RET_FAILED : self::RET_SUCCESS;
        $msg = !$em ? '邮箱可以使用' : '该邮箱已经存在';
        $this->ajaxRet($code, $msg);

    }

}